<?php
/**
 * LICENSE
 *
 * Copyright (C) 2010 Nicholas Miller
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Represents a specific endpoint
 *
 * @author     Nicholas Miller <nicholas.j.miller@gmail.com>
 * @copyright  2010 Nicholas Miller
 * @category   PaypalSoap
 */
class PayPalSoap_Endpoint
{
    /**
     * @var string
     */
    protected $_enviroment = '';

    /**
     * @var string
     */
    protected $_url = '';

    const ENV_LIVE         = 'live';
    const ENV_SANDBOX      = 'sandbox';
    const ENV_BETA_SANDBOX = 'beta-sandbox';
    const ENV_STAGE_2_EK   = 'stage2ek';

    const URL_API                = 'PayPalAPI';
    const URL_API_AA             = 'PayPalAPIAA';
    const URL_API_THREE_TOKEN    = 'PayPalAPI-threetoken';
    const URL_API_AA_THREE_TOKEN = 'PayPalAPIAA-threetoken';

    /**
     * Class Constructor
     *
     * @param string $environment ENV_* constant value
     * @param string $url         URL_* constant value
     * @throws InvalidArgumentException If $environment is not a valid ENV_* constant
     * @throws InvalidArgumentException If $url is not a valid URL_* constant
     */
    public function  __construct($environment, $url)
    {
        $validEnv = array(
            self::ENV_LIVE,
            self::ENV_SANDBOX,
            self::ENV_BETA_SANDBOX,
            self::ENV_STAGE_2_EK
        );

        $validUrl = array(
            self::URL_API,
            self::URL_API_AA,
            self::URL_API_AA_THREE_TOKEN,
            self::URL_API_THREE_TOKEN
        );

        if (!in_array($environment, $validEnv)) {
            throw new InvalidArgumentException(
                'The provided environment "' . $environment . '" is not valid'
            );
        }

        if (!in_array($url, $validUrl)) {
            throw new InvalidArgumentException(
                'The provided environment "' . $environment . '" is not valid'
            );
        }

        $this->_enviroment = $environment;
        $this->_url        = $url;
    }
    
    /**
     * __toString magic method. Used to represent the object as the endpoint url
     * @return string
     */
    public function  __toString()
    {
        $endpoints = $this->_getEndpoints();
        return $endpoints[$this->_enviroment][$this->_url];
    }

    /**
     * Creates the associative array of end points
     * @return array
     */
    protected function _getEndpoints()
    {
        return array (
            self::ENV_LIVE =>
            array (
                self::URL_API => 'https://api.paypal.com/2.0/',
                self::URL_API_AA => 'https://api-aa.paypal.com/2.0/',
                self::URL_API_THREE_TOKEN => 'https://api-3t.paypal.com/2.0/',
                self::URL_API_AA_THREE_TOKEN => 'https://api-aa-3t.paypal.com/2.0/',
            ),
            self::ENV_SANDBOX =>
            array (
                self::URL_API => 'https://api.sandbox.paypal.com/2.0/',
                self::URL_API_AA => 'https://api-aa.sandbox.paypal.com/2.0/',
                self::URL_API_THREE_TOKEN => 'https://api.sandbox.paypal.com/2.0/',
                self::URL_API_AA_THREE_TOKEN => 'https://api-aa.sandbox.paypal.com/2.0/',
            ),
            self::ENV_BETA_SANDBOX =>
            array (
                self::URL_API => 'https://api.beta-sandbox.paypal.com/2.0/',
                self::URL_API_AA => 'https://api-aa.beta-sandbox.paypal.com/2.0/',
                self::URL_API_THREE_TOKEN => 'https://api.beta-sandbox.paypal.com/2.0/',
                self::URL_API_AA_THREE_TOKEN => 'https://api-aa.beta-sandbox.paypal.com/2.0/',
            ),
            self::ENV_STAGE_2_EK =>
            array (
                self::URL_API => 'https://api.stage2ek.paypal.com/2.0/',
                self::URL_API_AA => 'https://api-aa.stage2ek.paypal.com/2.0/',
                self::URL_API_THREE_TOKEN => 'https://api.stage2ek.paypal.com/2.0/',
                self::URL_API_AA_THREE_TOKEN => 'https://api-aa.stage2ek.paypal.com/2.0/',
            ),
        );
    }

}